<?php

namespace App\Extend;

use JsonSerializable;

class ListWithCountStruct implements JsonSerializable
{
    /* @var \think\model\Collection */
	public $list = [];
	public $count = 0;

	public static function create($list, $count) {
		return (new self)->setList($list)->setCount($count);
	}

    public function each($callback) {
        /* @var $list \think\model\Collection */
        $list = $this->getList();
        if(!$list or $list->isEmpty()) {
            return $this;
        }

        $list->each($callback);
        $this->setList($list);
        return $this;
    }

	public function setList($list) {
		$this->list = $list;
		return $this;
	}

	public function setCount($count) {
		$this->count = $count;
		return $this;
	}

	public function getList() {
		return $this->list;
	}

	public function getCount() {
		return $this->count;
	}

	public function jsonSerialize()
	{
		return [
			'list' => $this->list,
			'count'=> $this->count,
		];
	}
}